﻿using System;
using Sys;
using System.Silverlight;
using System.Silverlight.Presentation;

namespace SBT
{
    public class AgLayoutManager : AgBehavior
    {
        private bool m_layoutSuspended = false;
        protected bool LayoutSuspended
        {
            get
            {
                return m_layoutSuspended;
            }
        }
        public void SuspendLayout()
        {
            m_layoutSuspended = true;
        }
        public void ResumeLayout()
        {
            m_layoutSuspended = false;
            InvalidateLayout();
        }
        private Canvas m_underlyingCanvas;
        protected Canvas UnderlyingCanvas { get { return m_underlyingCanvas; } }

        public void AddUIElement(UIElement item)
        {
            this.m_underlyingCanvas.Children.Add(item);
            OnUIElementAdded(item);
            InvalidateLayout();
        }

        public void RemoveUIElement(UIElement item)
        {
            this.m_underlyingCanvas.Children.Remove(item);
            OnUIElementRemoved(item);
            InvalidateLayout();
        }

        public void InsertUIElementAt(UIElement item, int index)
        {
            this.m_underlyingCanvas.Children.Insert(index, item);
            OnUIElementAdded(item);
            InvalidateLayout();
        }
        public void RemoveAt(int index)
        {
            UIElement item = m_underlyingCanvas.Children.GetItem(index);
            this.m_underlyingCanvas.Children.RemoveAt(index);
            OnUIElementRemoved(item);
            InvalidateLayout();
        }

        protected virtual void OnUIElementAdded(UIElement item)
        {

        }
        protected virtual void OnUIElementRemoved(UIElement item)
        {
        }
        
        public virtual void InvalidateLayout()
        {
        }
        public override void Initialize()
        {
            base.Initialize();
            m_underlyingCanvas = (Canvas)Element;
            try
            {
                UIElementCollection children = m_underlyingCanvas.Children;
            }
            catch
            {
                throw Exception.InvalidOperation("Xaml element attached to this behavior must be a Canvas");
            }
            InvalidateLayout();
        }

        
        protected DBounds GetItemBounds(int index)
        {
            UIElement elem = m_underlyingCanvas.Children.GetItem(index);
            AgBehavior behavior = AgBehavior.FindBehaviorByUIElement(elem);
            DBounds retval = new DBounds();
            if (behavior != null)
            {
                retval.Width = behavior.Width;
                retval.Height = behavior.Height;
                retval.Left = behavior.Left;
                retval.Top = behavior.Top;
            }
            else
            {
                try
                {
                    TextBlock asTb = (TextBlock)elem;
                    retval.Width =  asTb.ActualWidth;
                    retval.Height = asTb.ActualHeight;
                }
                catch
                {
                    retval.Width = elem.Width;
                    retval.Height = elem.Height;
                }
                retval.Left = (double)elem.GetValue("Canvas.Left");
                retval.Top = (double)elem.GetValue("Canvas.Top");
                
            }
            return retval;
        }
    }
}